<?php
namespace app\models;
use app\modules\company\models\Company;
use Yii;

class CompanyAccountRecordSearch extends CompanyAccountRecord
{
    public $begin_time;
    public $end_time;
    public $company_name;

    public function rules()
    {

        return [
            ['company_name', 'string', 'message' => 'company_name必须是字符串',
                             'max' => 64, 'tooLong' => 'company_name最长64位'],
            ['comid', 'string', 'message' => 'comid必须是字符串', 
                      'min' => 8, 'tooShort' => 'comid最短8位', 
                      'max' => 8, 'tooLong' => 'comid最长8位'],
            ['appid', 'string', 'message' => 'appid必须是字符串',
                      'min' => 18, 'tooShort' => 'appid最短18位',
                      'max' => 18, 'tooLong' => 'appid最长18位'],
            ['flag', 'integer', 'message' => 'flag必须为数字，或数字字符串'],
            ['flag', 'in', 'range' => array(1, 2, 3), 'message' => 'flag值必须在1-3中选择'],
            ['year', 'integer', 'message' => 'year必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'year最小存储0', 
                        'max'=>99, 'tooBig' => 'year最大存储99'],
            ['paymode', 'integer', 'message' => 'paymode必须为数字，或数字字符串'],
            ['paymode', 'in', 'range' => array(1, 2, 3), 'message' => 'paymode值必须在1-3中选择'],
            ['begin_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'begin_time非yyyy-MM-dd格式'],
            ['end_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'end_time非yyyy-MM-dd格式'],
        ];
    }

    public function search($params)
    {
        $data['CompanyAccountRecordSearch'] = $params;
        // P($data);

        $query = CompanyAccountRecord::find();

        if (!($this->load($data) && $this->validate())) {
            return false;
        }
        // P($this);

        if(isset($this->company_name)){
            /*根据企业名称 取得comid*/
            $company = Company::find()
                                ->select('comid')
                                ->where(['like', '{{%company}}.company_name', $this->company_name])
                                ->asArray()
                                ->all();
            // P($company);
            $comidArr = [];
            if(count($company)){
                foreach($company as $k => $v){
                    array_push($comidArr, $v['comid']);
                    
                }
            }
            // P($comidArr);
            if(count($comidArr)){
                $query -> andFilterWhere(['in', 'comid', $comidArr]);
            }
        }

        $query -> andFilterWhere([
            'appid' => $this->appid,
        ]);

        $query -> andFilterWhere([
            'flag' => $this->flag,
        ]);

        $query -> andFilterWhere([
            'year' => $this->year,
        ]);

        $query -> andFilterWhere([
            'paymode' => $this->paymode,
        ]);

        $query -> andFilterWhere(['>=', '{{%company_account_record}}.add_time', $this->begin_time])
               -> andFilterWhere(['<=', '{{%company_account_record}}.add_time', $this->end_time]);

        // P($query);

        return $query;
    }


}